<?php  
class News_UI_Downloads
{
    var $Controller;
    function News_UI_Downloads($controller)
    {  
    
        $this->Controller = $controller;   
        $this->Bookmarks = $this->GetBookmarks(); 
    } 
    
    function GetBookmarks()
    { 
        $dir       = 'data/perm/';
        $groupdirs = $this->Controller->OpenDir($dir); 
        $ret       = array();
        for ($x=0;$x<sizeof($groupdirs);$x++)
        { 
            $name         = $groupdirs[$x]['name'];
            $markdirs     = $this->Controller->OpenDir ($groupdirs[$x]['path'] . '/');
            $ret[$name]   = array();
            for ($i=0;$i<sizeof($markdirs);$i++)
            { 
                $ret[$name][$markdirs[$i]['name']] = $this->Controller->OpenDir ($markdirs[$i]['path'] . '/');
            }
            
        }
        return $ret;
    }
//    
//    function OpenDir ($dir)
//    { 
//        $array = array();  
//        if (is_dir($dir)) {
//            if ($dh = opendir($dir)) {
//                while (($file = readdir($dh)) !== false) {
//                    if (!($file=='.'||$file=='..'))
//                    { 
//                        $array[] = array('path'=>$dir . $file, 'name'=>$file);
//                    }
//                }
//                closedir($dh);
//            }
//        }
//        return $array;
//    }
//    
    function Display ()
    { 
//		echo "<html>" . 
//		     " <head>" . 
//		     "   <script language='javascript' src='/images.js?465'></script>" . 
//		     "   <script language='javascript' src='http://www.101stratford.com/101/ebp6/js/xmlextras.js'></script>" . 
//		     "   <link rel='stylesheet' href='/nntp.css?123' type='text/css'/>" . 
//		     " </head>" . 
//		     "<body onload='attach()'>";      
//		
		$data               = array();
		$data['toolbar']    = $this->Controller->Factory->TabBar('Downloads');
        $data['header']     = "<a href='/ui/'>Home</a> &#187;";
        $data['main']       = '';
		$page  = $this->Controller->Settings['page'];
	    if (strlen($page) == 0) $page = 1;
	    
	    $pane  = $this->Controller->Settings['pane'];
	    if (strlen($pane) == 0) $pane = 1;
	    
	    $picindex  = $this->Controller->Settings['item'];
	    if (strlen($picindex) == 0) $picindex = 0;
	    
	    
		if (isset($this->Controller->Settings['edit']))
		{   
		    $group   = $this->Controller->Settings['group'];
		    $id      = $this->Controller->Settings['edit'];
		    $gallery = $this->Bookmarks[$group][$id];
		    $count   = sizeof($gallery);
		    $value   = $gallery[0]['name'];
	    	$hi      = $pane * UI_PAGE_SIZE;
	    	$low     = $hi - UI_PAGE_SIZE;
		
            $back    = $this->Controller->URI (array('group'   => NULL,
                                                     'edit'    => NULL,
                                                     'pane'    => NULL,
                                                     'id'      => NULL));
//	    	ripple sort
	        for($x = 0; $x < sizeof($gallery); $x++) {
			  for($y = 0; $y < sizeof($gallery); $y++) {
			    if(strcmp($gallery[$x]['path'], $gallery[$y]['path']) < 0) {
			      $hold = $gallery[$x];
			      $gallery[$x] = $gallery[$y];
			      $gallery[$y] = $hold;
			    }
			  }
			}
			
		    $data['header'] .= " <a href='{$back}'>Downloads</a> &#187; ";
		    $data['header'] .= "<a href='/ui/commandhandler/request/group/group/{$group}'>{$group}</a> &#187; {$value}";
            $data['header'] .= "<div style='float:right'>";  
            $data['header'] .= "<a href='javascript:void(0)' onclick='$(this.className).submit()' class='sub'>Delete selected</a></div>";
            
            $uri = $this->Controller->URI (array('ui'      => 'commandhandler',
                                                 'request' => 'fileedit',
                                                 'group'   => $group, 
                                                 'pane'    => $pane, 
                                                 'page'    => $page, 
                                                 'edit'    => $id  ));

            $data['main'] .= "<form id='sub'  action='{$uri}' method='post'>";
            for($x=$low;$x<$hi&&$x<sizeof($gallery);$x++)
            {
                $item    = $gallery[$x];
		        $picname = $gallery[$x]['path']; 
                $uri     = $this->Controller->URI(array('group'   => $group, 
                                                        'id'      => $id,
                                                        'item'    => $x,
                                                        'edit'    => NULL)); 
                
		        $box     = "<input type='checkbox' value='{$picname}' id='x_{$name}' name='rec[]'>";
		        $picname = str_replace ('/', "^", $picname);
                $picname = "/ui/image/id/resize/name/$picname";
                        
                $data['main'] .= $this->Controller->Factory->Thumbnail ($picname, $uri, $box . $gallery[$x]['name']); 
            } 
            $data['main'] .= "</form>";
            
            $data['controlbar'] = $this->Controller->Factory->Paginator(sizeof($gallery), array('pane' => NULL), 'pane');
	    	echo $this->Controller->Structure ($data);
		    return;
            
            
	    	
		} 
	    
		if (isset($this->Controller->Settings['group']))
		{    
		    $group = $this->Controller->Settings['group'];
//		    if (isset($this->Controller->Settings['remove']))
//		    {
//		        $id = $this->Controller->Settings['remove'];
//		        $this->RemoveBookmark($group, $id);
//		        redirect ("/ui/downloads/page/{$page}");
//		    }
		    $id    = $this->Controller->Settings['id'];
		    $gallery = $this->Bookmarks[$group][$id];
		    uasort($gallery, 'cmpz');
		    $picname = $gallery[$picindex]['path']; 
            $uri   = $this->Controller->URI (array('group'=>NULL, 'id'=>NULL));                
            $data['header']     .= "<a href='{$uri}'>Downloads</a> &#187; " . 
		                           "<a href='/ui/commandhandler/request/group/group/{$group}'>{$group}</a> &#187; {$id}";  
		    
            $data['controlbar'] = "<select id='drp' onchange='view(this.options[this.selectedIndex].value)'>";  
            $z = 0;
            foreach($gallery as $item)
            {
                $src = $item['path']; 
                $data['controlbar'] .= sprintf("<option value='/%s' %s>%s</option>", $item['path'], $z==$picindex?'selected':'', $item['name']); 
                $z ++;
            }
            
            $data['controlbar'] .= "</select>";
            $data['controlbar'] .= "<input type='button' value='Prev' onclick='advance(-1)' />";
            $data['controlbar'] .= "<input type='button' value='Next' onclick='advance(1)' />";
            $data['controlbar'] .= "<input type='button' id='btn' value='Play' onclick='Play(true)' /><br/>";
            
            $data['main'] .= "<center>";
            if (file_exists($picname))
            {
                $data['main'] .= "<a href='javascript:void(0)' onclick='advance(1)'><img id='box' border='0' height='540' src='/{$picname}' /></a><br/>";
            }
            $data['main'] .= "</center>"; 
	    	echo $this->Controller->Structure ($data);
		    return;
		}
    
		$count = 0; $e=0;
        while (list($name, $value) = each ($this->Bookmarks))
        { 
            while (list($key, $val) = each ($value))
            {   
                $count++;
            }
        } 
		 
        $data['header']    .= " Downloads "; 
        $data['controlbar'] = $this->Controller->Factory->Paginator($count, array('ui'=>'downloads', 'page'=>NULL)); 
        
		$num    = $page * UI_PAGE_SIZE;
		$min    = $num - UI_PAGE_SIZE;
		$i      = 0;    
		reset ($this->Bookmarks);
        while (list($name, $value) = each ($this->Bookmarks))
        { 
            while (list($key, $val) = each ($value))
            {  
                if ($i >= $min && $i < $num)
                {
                    $picfile = $val[0]['name'];
                    $picname = $val[0]['path']; 
                    $size    = sizeof($val);
                    $error   = "Could not load article {$picname}<br/>";
                    $uri     = $this->Controller->URI(array('group'   => $name, 
                                                            'id'      => $key)); 
                    $ask     = $this->Controller->URI(array('group'   => $name, 
                                                            'edit'    => $key,
                                                            'pane'    => 1)); 
                    $rm      = $this->Controller->URI(array('ui'      => 'commandhandler', 
                                                            'request' => 'remove', 
                                                            'page'    => $page, 
                                                            'group'   => $name, 
                                                            'index'   => $key)); 
                    $edit    = $this->Controller->Factory->Button ($rm, 'Delete', '/images/i.p.delete.gif', "Are you sure you want to delete the selected files?");
                    $view    = $this->Controller->Factory->Button ($ask, 'Browse', '/images/i.p.edit.gif');
                    $caption = "{$view}{$edit}<div align='center'><b>{$name}</b></div>{$picfile}<br/>{$size} articles";
                    if (file_exists($picname))
                    {     
                        $picname = "{$picname}";
                        $picname = str_replace ('/', "^", $picname);
                        $picname = "/ui/image/id/resize/name/$picname";
                        $error   = NULL;
                    }
                    $data['main'] .= $this->Controller->Factory->Thumbnail ($picname, $uri, $caption, $error); 
                }
                $i++;
            }
        }
		echo $this->Controller->Structure ($data);
        return;
        var_dump ($this->Bookmarks);
    }
}



function cmpz($a, $b) {  
     
    return strcmp ($a['name'], $b['name']);
    return ($a['name'] > $b['name']) ? -1 : 1;
}